home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4913 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  71 lines

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Comparing Struct elements
  5. Date: Thu, 01 Feb 1996 16:34:11 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4eqq15$cgt@news.halcyon.com>
  8. References: <4eo1va$s6k@camelot.ccs.neu.edu>
  9. NNTP-Posting-Host: blv-pm2-ip25.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. What's happening is you're comapring the address of the
  13. ClassRec::Grade[] string for the LetGrade instance with the address of
  14. the string constant "A", "A-", etc.  Naturally, the addresses will
  15. never be the same.
  16.  
  17. You should write equaility functions, like so
  18.  
  19. class ClassRec {
  20. public:
  21.     ...
  22.     BOOL  operator==( const char * const lpszGrade)
  23.     {  return( 0 == lstrcmpi(Grade, lpszGrade) ); }
  24.     ...
  25.  
  26. private:
  27.     char Grade[3];
  28.     ...
  29. };
  30.  
  31. Then write   if( LetGrade == "A")  gpa=4.0F;  etc.
  32.  
  33.                     --Norm
  34.  
  35.  
  36.  
  37. bhardwaj@ccs.neu.edu (saurabh bhardwaj) wrote:
  38.  
  39. >Hello,
  40.  
  41. >I've been trying to compare structure elements using the if Statement.  Here
  42. >is the code:
  43.  
  44.  
  45. >double NumericVal(ClassRec& LetGrade){
  46.  
  47. >cout << "assgning struct member usin . " << LetGrade.
  48. >Grade << '\n';
  49. >if(LetGrade.Grade == "A") return(4.000);
  50. >else if(LetGrade.Grade == "A-") return(3.666);
  51. >else if(LetGrade.Grade == "B+") return(3.333);
  52. >else return 0;
  53. >}
  54.  
  55. >where LetGrade is the following struct:
  56.  
  57. >struct ClassRec {
  58. >     char Grade[3];
  59. >     int   QH;
  60. >     char Name[10];
  61. >     };
  62.  
  63. >I can't seem to compare the elements of the Grade[3] with "A" or "A-" or "B+"
  64. >The function invariable returns 0.  I've also tried using the -> operator.  It
  65. >doesn't work either!!!   Anyone know why???  I'll appreciate any help.
  66.  
  67. >                        thanks
  68. >                        saurabh
  69.  
  70.  
  71.